package biz.ezcom.hibernate.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import biz.ezcom.hibernate.pojo.User;
import biz.ezcom.hibernate.pojo.UserId;

public class Client {
	@Test
	public void testSave(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			UserId userId = new UserId();
			userId.setUsername("Felix");
			userId.setPassword("password");
			
			User user = new User();
			user.setId(userId);
			user.setIsManager(false);
			session.save(user);
			
			tx.commit(); 
		}catch(Exception e){
			e.printStackTrace();
			tx.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	@Test
	public void testGet(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();

			UserId userid = new UserId();
			userid.setUsername("Felix");
			userid.setPassword("password");
			
			User user = (User)session.get(User.class, userid);
			System.out.println(user.getIsManager());
			
			UserId userId = user.getId();
			System.out.println(userId.getUsername());
			System.out.println(userId.getPassword());
			
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			tx.rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
}
